# coding:utf8
"""
Zeander Common Module
"""

import os
import time
import ConfigParser

__author__ = 'Zeander'
__CONFIG__ = 'app.conf'
__CONFIG__DEBUG__ = 'app.conf.debug'


class ZLOG(object):
    """ZLog Class"""
    EMERG = 0
    ALERT = 1
    CRIT = 2
    ERROR = 3
    WARN = 4
    NOTICE = 5
    INFO = 6
    DEBUG = 7

    Priority = {0: 'EMERG', 1: 'ALERT', 2: 'CRIT', 3: 'ERROR',
                4: 'WARN', 5: 'NOTICE', 6: 'INFO', 7: 'DEBUG'}

    @staticmethod
    def log(msg, pri=6):
        """display log"""
        if pri <= ZLOG.check_debug_level():
            msg = '[%s][%s]:%s' % (ZLOG.Priority[pri], time.strftime('%Y-%m-%d %H:%M:%S'), msg)
            print msg

    @staticmethod
    def check_debug_level():
        """Check log display devel"""
        if os.path.exists('debug.%s' % ZLOG.Priority[0].lower()):
            return ZLOG.EMERG
        if os.path.exists('debug.%s' % ZLOG.Priority[1].lower()):
            return ZLOG.ALERT
        if os.path.exists('debug.%s' % ZLOG.Priority[2].lower()):
            return ZLOG.CRIT
        if os.path.exists('debug.%s' % ZLOG.Priority[3].lower()):
            return ZLOG.ERROR
        if os.path.exists('debug.%s' % ZLOG.Priority[4].lower()):
            return ZLOG.WARN
        if os.path.exists('debug.%s' % ZLOG.Priority[5].lower()):
            return ZLOG.NOTICE
        if os.path.exists('debug.%s' % ZLOG.Priority[7].lower()):
            return ZLOG.DEBUG
        return ZLOG.INFO

    @staticmethod
    def emrg(msg):
        """log message in emergent level"""
        ZLOG.log(msg, ZLOG.EMERG)

    @staticmethod
    def alert(msg):
        """log message in alert level"""
        ZLOG.log(msg, ZLOG.ALERT)

    @staticmethod
    def crit(msg):
        """log message in critical level"""
        ZLOG.log(msg, ZLOG.CRIT)

    @staticmethod
    def error(msg):
        """log message in error level"""
        ZLOG.log(msg, ZLOG.ERROR)

    @staticmethod
    def warn(msg):
        """log message in warning level"""
        ZLOG.log(msg, ZLOG.WARN)

    @staticmethod
    def notice(msg):
        """log message in notice level"""
        ZLOG.log(msg, ZLOG.NOTICE)

    @staticmethod
    def info(msg):
        """log message in information level"""
        ZLOG.log(msg, ZLOG.INFO)

    @staticmethod
    def debug(msg):
        """log message in debug level"""
        ZLOG.log(msg, ZLOG.DEBUG)


def get_config_obj():
    """Get ConfigParser Object"""
    cp_obj = ConfigParser.SafeConfigParser(allow_no_value=True)
    if os.path.exists(__CONFIG__DEBUG__):
        cp_obj.read(__CONFIG__DEBUG__)
    elif os.path.exists(__CONFIG__):
        cp_obj.read(__CONFIG__)
    else:
        ZLOG.log("Configure file missed!", ZLOG.ERROR)
    return cp_obj
